package com.example.shaojun.fragmentliefcycle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button button;
    RightFragment rightFragment;
    AnotherRightFragment anotherRightFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
        rightFragment = new RightFragment();
        anotherRightFragment = new AnotherRightFragment();
        replaceFragment(rightFragment);
    }

    @Override
    public void onClick(View view) {
        replaceFragment(anotherRightFragment);
    }

    private void replaceFragment(Fragment f){
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.right_layout,f);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}
